import tensorflow as tf
import numpy as np

x = tf.constant([[1], [2], [3], [4]], dtype=tf.float32)
y_true = tf.constant([[0],[-1], [-2], [-3]], dtype=tf.float32)
linear_model = tf.layers.Dense(units=1)
y_pred = linear_model(x)
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
print('y_pred:\n', sess.run(y_pred))

loss = tf.losses.mean_squared_error(labels=y_true, predictions=y_pred)
print('loss val:', sess.run(loss))

optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
for i in range(100):
    _, loss_value = sess.run((train, loss))
    if i%10 == 0:
        print('iter: %f loss_val: %f'%(i, loss_value))

print('y_pred after train:', sess.run(y_pred))